package com.shadow.demo.decompilation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Step3BuildDir {

    public static int INDEX = 1;
    public static final List<String> NEED = new ArrayList<>();

    public static String PROJECT_NAME = null;
    public static File COM = null;
    public static File POM = null;

    public static void main(String[] args) {
        File root = new File(Config.TARGET2_DIRECTORY);
        if (!root.exists() || !root.isDirectory()) {
            System.out.println("路径无效");
            return;
        }
        File[] files = root.listFiles();
        if (files == null) {
            System.out.println("空");
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("-----第" + INDEX + "个: " + file.getName());
                PROJECT_NAME = file.getName();
                getProject(file);
            } else if (file.isFile()) {
                System.out.println("不应该啊, 上一步异常");
            }
            buildProject();
            PROJECT_NAME = null;
            COM = null;
            POM = null;
            INDEX++;
        }
        System.out.println("----------end");
        if (!NEED.isEmpty()) {
            System.out.println(NEED);
        }
    }

    public static void getProject(final File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            System.out.println("不应该啊, 上一步异常2");
            return;
        }

        if (files.length == 1 && files[0].isFile() && files[0].getName().equals("pom.xml")) {
            System.out.println("pom依赖");
            POM = files[0];
        } else {
            for (File file : files) {
                if (file.isDirectory()) {
                    if (file.getName().equals("com")) {
                        COM = file;
                    } else if (file.getName().equals("META-INF")) {
                        meta(file);
                    } else {
                        NEED.add("不在识别的目录结构中: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }

    public static void meta(File file) {
        File[] files = file.listFiles();
        if (files == null) {
            NEED.add("meta: " + file.getAbsolutePath());
            return;
        }
        for (File dir : files) {
            if (file.isDirectory() && dir.getName().equals("maven")) {
                toPom(dir);
            } else if (dir.isFile() && dir.getName().equals("MANIFEST.MF")) {
                // ...
            } else {
                NEED.add("meta: " + file.getAbsolutePath());
            }
        }
    }

    public static void toPom(File dir) {
        File[] files = dir.listFiles();
        if (files != null && files.length == 1 && files[0].isDirectory()) {
            toPom(files[0]);
        } else if (files != null && files.length > 1) {
            for (File d : files) {
                if (d.isFile() && d.getName().equals("pom.xml")) {
                    POM = d;
                } else if (d.isFile() && d.getName().equals("pom.properties")) {
                    // ...
                } else {
                    NEED.add("toPom: " + d.getAbsolutePath());
                }
            }
        }
    }

    // 构造项目
    public static void buildProject() {
        File projectDir = new File(Config.TARGET3_DIRECTORY);
        if (Objects.isNull(PROJECT_NAME)) {
            return;
        }
        File project = FileUtils.mkdir(projectDir, PROJECT_NAME);
        if (project == null) {
            return;
        }
        System.out.println("---复制pom");
        FileUtils.copyFile(POM, new File(project, "pom.xml"));
        if (Objects.isNull(COM)) {
            return;
        }
        File src = FileUtils.mkdir(project, "src");
        if (src == null) {
            return;
        }
        File main = FileUtils.mkdir(src, "main");
        if (main == null) {
            return;
        }
        File java = FileUtils.mkdir(main, "java");
        if (java == null) {
            return;
        }
        System.out.println("---复制com");
        FileUtils.copyAll(COM, java);
    }
}
